using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Pix2Gallery2
{
    public partial class OptionsForm : Form
    {
        private Pix2Gallery2Settings m_pix2gallery2Settings;

        public Pix2Gallery2Settings pix2gallery2Settings
        {
            get 
            {
                return m_pix2gallery2Settings;
            }
            set
            {
                m_pix2gallery2Settings = value;
            }
        }


        public OptionsForm()
        {
            InitializeComponent();
        }

        private void OptionsForm_Load(object sender, EventArgs e)
        {
            if (this.m_pix2gallery2Settings == null)
            {
                MessageBox.Show("ERROR: Settings not loaded!");
                this.Close();
            }

            LoadTheListBox();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            GalleryURLForm urlForm = new GalleryURLForm();
            urlForm.pix2gallery2Settings = this.pix2gallery2Settings;
            urlForm.ShowDialog();

            LoadTheListBox();
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            GalleryURLForm urlForm = new GalleryURLForm();
            urlForm.pix2gallery2Settings = this.pix2gallery2Settings;
            //Update to be the chosen item from the list box...
            urlForm.textBoxGalleryTitle.Text = this.listBoxRegisteredGalleries.SelectedItem.ToString();
            urlForm.ShowDialog();

            LoadTheListBox();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            foreach (Gallery2Url g2Url in this.m_pix2gallery2Settings.Gallery2Urls)
            {
                if (g2Url.m_GalleryTitle == this.listBoxRegisteredGalleries.Text)
                {
                    this.m_pix2gallery2Settings.Gallery2Urls.Remove(g2Url);
                    this.m_pix2gallery2Settings.AppSettingsChanged = true;
                    break;
                }
            }
            LoadTheListBox();
        }

        private void LoadTheListBox()
        {
            //Load the Listbox
            if (m_pix2gallery2Settings.Gallery2Urls != null)
            {
                this.listBoxRegisteredGalleries.BeginUpdate();
                this.listBoxRegisteredGalleries.Items.Clear();
                foreach (Gallery2Url G2Url in m_pix2gallery2Settings.Gallery2Urls)
                {
                    this.listBoxRegisteredGalleries.Items.Add(G2Url.m_GalleryTitle);
                }
                this.listBoxRegisteredGalleries.EndUpdate();
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.pix2gallery2Settings.SaveAppSettings();
            if (this.pix2gallery2Settings.lastErrorMessage != "")
            {
                MessageBox.Show("Error: " + this.pix2gallery2Settings.lastErrorMessage);
            }
            this.Close();
        }

        private void listBoxRegisteredGalleries_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.buttonEdit.Enabled = true;
            this.buttonDelete.Enabled = true;
            this.buttonDefaultUrl.Enabled = true;
        }

        private void buttonDefaultUrl_Click(object sender, EventArgs e)
        {
            if (this.listBoxRegisteredGalleries.SelectedItem == null)
            {
                MessageBox.Show("Error: No URL selected!");
            }
            else
            {
                this.pix2gallery2Settings.DefaultGalleryUrl = this.listBoxRegisteredGalleries.SelectedItem.ToString();
                this.m_pix2gallery2Settings.AppSettingsChanged = true;
            }
        }
    }
}
